package com.king.java.tools.http.client.okhttp.impl;


import com.king.java.tools.http.HttpUtils;
import com.king.java.tools.http.client.okhttp.AbstractOkhttp;
import com.king.java.tools.http.entity.ConnectEntity;
import com.king.java.tools.http.entity.HttpJsonEntity;
import com.king.java.tools.http.entity.HttpMethod;
import com.king.java.tools.utils.StringUtils;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 描述:
 *
 * @author : sujinchen
 * @date : 2020/10/16
 */
public class DeleteHttp extends AbstractOkhttp implements HttpUtils {
    public DeleteHttp(OkHttpClient client, HttpJsonEntity entity) {
        super(client, entity);
    }

    public DeleteHttp(HttpJsonEntity entity) {
        super(entity);
        entity.setMethod(HttpMethod.DELETE);

    }

    @Override
    public ConnectEntity run() throws Exception {
        Request request;
        if (!StringUtils.isEmpty(entity.getReqBody())) {
            MediaType JSON = MediaType.parse(entity.getMediaType());
            RequestBody requestBody = RequestBody.create(JSON, entity.getReqBody());
            request = new Request.Builder()
                    .url(entity.getUrl())
                    .delete(requestBody)
                    .build();
        } else {
            request = new Request.Builder()
                    .url(entity.getUrl())
                    .delete()
                    .build();
        }



        return synCall(request);
    }
}
